package jdsk.iria;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class StatsActivity extends Activity {

	private static String studentName;
	private static String fileName;
	private static String teacherName;
	private static String CB;
	private static String notes;
	private static String spinner1;
	private static String spinner2;
	private static String spinner3;
	private double countOfGestures;
	private double numOfWords;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_stats);
		((EditText) findViewById(R.id.editText1)).setKeyListener(null);
		((EditText) findViewById(R.id.editText2)).setKeyListener(null);
		((EditText) findViewById(R.id.editText3)).setKeyListener(null);
		((EditText) findViewById(R.id.editText4)).setKeyListener(null);

		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			studentName = extras.getString("studentName");
			fileName = extras.getString("fileName");
			teacherName = extras.getString("teacherName");
			spinner1 = extras.getString("spinner1");
			spinner2 = extras.getString("spinner2");
			spinner3 = extras.getString("spinner3");
			notes = extras.getString("notes");
			CB = extras.getString("CB");
			countOfGestures = extras.getInt("countOfGestures");
			numOfWords = extras.getInt("numOfWords");
		}

		((EditText) findViewById(R.id.editText1)).setText(String
				.valueOf(numOfWords));

		((EditText) findViewById(R.id.editText2)).setText(String
				.valueOf(numOfWords - countOfGestures));

		((EditText) findViewById(R.id.editText3)).setText(String
				.valueOf(countOfGestures));

		((EditText) findViewById(R.id.editText4)).setText(String
				.valueOf(((numOfWords - countOfGestures) / numOfWords) * 100)
				+ "%");

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_stats, menu);
		return true;
	}

	public void saveStatsButtonClick(View view) {

		try {

			File mediaStorageDir = new File(
					Environment.getExternalStorageDirectory(), "IRIA");
			File mediaFile = new File(mediaStorageDir.getPath()
					+ File.separator + fileName + ".txt");

			FileOutputStream fOut = new FileOutputStream(mediaFile);
			OutputStreamWriter osw = new OutputStreamWriter(fOut);

			osw.write("\n\t\t\t\t\t\tReport Card for -->  " + studentName);
			osw.write("\n\tAdministered by: " + teacherName);
			osw.write("\n\n\t------- Stats");
			osw.write("\n\t*******************************************************************");
			osw.write("\n\tTotal Words:\t\t\t\t" + "[" + numOfWords + "]");
			osw.write("\n\tWords Correct:\t\t\t["
					+ (numOfWords - countOfGestures) + "]");
			osw.write("\n\tWords Incorrect:\t\t[" + countOfGestures + "]");
			osw.write("\n\tPercentage:\t\t\t\t["
					+ (((numOfWords - countOfGestures) / numOfWords) * 100)
					+ "%]");
			osw.write("\n\t*******************************************************************");
			osw.write("\n\n");
			osw.write("\n\t------- Analysis of Retelling");
			osw.write("\n\t*******************************************************************");
			osw.write("\n\t" + CB);
			osw.write("\n\n\tNotes:");
			osw.write("\n\t" + notes);
			osw.write("\n\t*******************************************************************");
			osw.write("\n\n");
			osw.write("\n\t------- Fluency Rubric");
			osw.write("\n\t*******************************************************************");
			osw.write("\n\tExpression:");
			osw.write("\n\t\t" + spinner1);
			osw.write("\n\n\tPhrasing and Smoothness:");
			osw.write("\n\t\t" + spinner2);
			osw.write("\n\n\tPace:");
			osw.write("\n\t\t" + spinner3);
			osw.write("\n\t*******************************************************************");
			osw.flush();
			osw.close();

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		Toast.makeText(this, "File Successfully Saved To: [" + fileName + "]",
				Toast.LENGTH_LONG).show();

		Intent i = getBaseContext().getPackageManager()
				.getLaunchIntentForPackage(getBaseContext().getPackageName());
		i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		startActivity(i);

	}

}
